/*
 * @Author: 徐建辰
 * @Date: 2021-11-23 14:29:32
 * @LastEditTime: 2021-11-23 15:54:48
 * @LastEditors: Please set LastEditors
 * @Description: webpack基本配置，开发生产共享的配置
 */
const path = require('path')
const {VueLoaderPlugin} = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
  entry: path.resolve(__dirname, '../src/main.js'),
  output: {
    path: path.resolve(__dirname, '../dist'),
    // 根据资源内容创建出唯一hash
    filename: '[name].[contenthash:8].js',
    clean: true // 每次构建清除dist包
  },
  // 插件配置
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../public/index.html'),
      filename: 'index.html',
    }),
    new MiniCssExtractPlugin({
      filename: "static/css/[name].css"
    })
  ],
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.vue', '.ts', '.tsx'],
    alias: {
      "@": path.resolve(__dirname, '../src'),
    },
  },
  // 配置cdn
  externals: {
    'vue': 'Vue',
    'vue-router': 'VueRouter'
  },
  module: {
    rules: [
      // vue-loader必须要放在匹配规则的第一个，否则会报错
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        include: [path.resolve(__dirname, '../src')]
      },
      {
        test: /(\.jsx|\.js)$/,
        use: ['babel-loader'],
        exclude: /node_modules/,
      },
      // 处理样式文件
      {
        test: /\.(css|scss|sass)$/,
        use: [ MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader', {
          loader: 'postcss-loader',
          options: {
            postcssOptions: {
              plugins: ['autoprefixer']
            },
          }
        }],
      },
      // 如果你想配置less可以使用下面的代码，前提是你已经安装了这些依赖
      // {
      //   test: /\.(css|less)$/,
      //   use: [MiniCssExtractPlugin.loader, 'css-loader', {
      //       loader: 'postcss-loader',
      //       options: {
      //           postcssOptions: {
      //               plugins: [
      //                   require('postcss-preset-env')({
      //                       autoprefixer: {flexbox: 'no-2009'},
      //                       stage: 3//兼容程度
      //                   }),
      //                   require('postcss-normalize')()//兼容老旧版本的浏览器
      //               ]
      //           }
      //       }
      //   }, 'less-loader']
      // },
      
      // 使用内置的asset处理资源文件
      {
        type: 'asset/resource',
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        generator: {
          filename: 'images/[name].[hash:5][ext]'
        },
      }
    ]
  }
}